package com.lanou.tipsy.fragment.mv;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.mv.MvAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.base.Constants;
import com.lanou.tipsy.entity.mv.MusicMv;
import com.lanou.tipsy.entity.mv.MvListEntity;
import com.lanou.tipsy.tools.formatCodeUtil.FormatCodeUtil;
import com.lanou.tipsy.tools.popupWindow.SelectPicPopupWindow;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * Created by wangshuai on 15/10/30.
 */
public class MvFragment extends BaseFragment implements AdapterView.OnItemClickListener {
    public static final String TAG = "MvFragment";

    private View v;
    //自定义的弹出框类
    private SelectPicPopupWindow menuWindow;
    //显示菜单窗口按钮
    private Button popupWindowBtn;
    //声明musicMvGv
    private GridView musicMvGv;
    //建立队列
    private RequestQueue queue;
    //实体类 接受数据
    private ArrayList<MvListEntity> mvListEntities;
    //声明适配器
    private MvAdapter adapter;
    private String url;
    private String mv_id;//mv传值id
    private FragmentManager message;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fr_mv, null);
        popupWindowBtn = (Button) v.findViewById(R.id.popupWindowBtn);
        //把文字控件添加监听，点击弹出自定义窗口
        popupWindowBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //实例化SelectPicPopupWindow
                menuWindow = new SelectPicPopupWindow(getActivity(), itemsOnClick);
                //显示窗口,设置layout在PopupWindow中显示的位置
                menuWindow.showAtLocation(v, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
                //销毁窗口的时候,背景回复正常
                menuWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        Log.i("test", TAG + "dismiss");
                        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
                        lp.alpha = 1f;
                        getActivity().getWindow().setAttributes(lp);
                    }
                });
            }
        });
        init();
        return v;
    }

    private void init() {

        musicMvGv = (GridView) v.findViewById(R.id.musicMvGv);
        //行布局进行监听
        musicMvGv.setOnItemClickListener(this);
        //从控件获取内容
        String name = popupWindowBtn.getText().toString();
        //进行转码
        String sr = FormatCodeUtil.codingFormat(name);
        //进行接口拼接
        String url = Constants.MV_URL + sr;
        //初始化队列
        queue = Volley.newRequestQueue(getContext());
        //进行网络数据拉取
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //gson解析
                Gson gson = new Gson();
                //最外层实体类
                MusicMv musicMv = gson.fromJson(response, MusicMv.class);
                //接收数据
                mvListEntities = musicMv.getResult().getMv_list();
                //适配器适配数据
                adapter = new MvAdapter(getContext(), mvListEntities);
                //你绑定适配器
                musicMvGv.setAdapter(adapter);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        //添加队列
        queue.add(stringRequest);
    }

    //为弹出窗口实现监听类
    private View.OnClickListener itemsOnClick = new View.OnClickListener() {
        public void onClick(View v) {
            //点击相应后关掉窗口的方法
            menuWindow.dismiss();
            String name = "";
            switch (v.getId()) {
                case R.id.HKTBtn:
                    name = "港台";
                    break;
                case R.id.EUSBtn:
                    name = "欧美";
                    break;
                case R.id.japanBtn:
                    name = "日本";
                    break;
                case R.id.koreaBtn:
                    name = "韩国";
                    break;
                case R.id.scenceBtn:
                    name = "现场";
                    break;
                case R.id.chinaBtn:
                    name = "内地";
                    break;
                case R.id.allMvBtn:
                    name = "全部";
                    break;
                case R.id.cancelBtn: //取消按钮
                    break;

            }
            popupWindowBtn.setText(name);

            refreshData(name);
        }
    };

    public void refreshData(String name) {
        try {
            String enName = URLEncoder.encode(name, "utf-8");
            url = Constants.MV_URL + enName;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                //gson解析
                Gson gson = new Gson();
                //最外层实体类
                MusicMv musicMv = gson.fromJson(response, MusicMv.class);
                //接收数据
                mvListEntities = musicMv.getResult().getMv_list();
                adapter.refreshData(mvListEntities);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(stringRequest);
    }

    //mv播放监听
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // mv_id = mvListEntities.get(position).getMv_id();//获得数据
        MvListEntity mvListEntity = (MvListEntity) parent.getItemAtPosition(position);
        mv_id = mvListEntity.getMv_id();
        message = getActivity().getSupportFragmentManager();
        getActivity().overridePendingTransition
                (R.anim.translate_enter_in, R.anim.translate_enter_out);
        MvVideoInfoFragment mvVideoFragment = new MvVideoInfoFragment();
        Bundle bundle = new Bundle();
        bundle.putString("mv_id", mv_id);
        mvVideoFragment.setArguments(bundle);
        FragmentTransaction transaction = message.beginTransaction();
        transaction.addToBackStack(null);
        transaction.replace(R.id.fl_main, mvVideoFragment);
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.commit();
    }
}
